package com.lili.collection.core.domain;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

/**
 *描述: 运行信息传递
 *@date:2023/4/28  16:59
 *@author:lili
 */
@Data
@Slf4j
public class RunnerMsgTransfer implements Serializable {
    private static final long serialVersionUID = -5763095632142361063L;

    /**
     * 是否是错误的
     */
    private volatile boolean isRunError=false;

    /**
     * 删除的偏移数
     */
    private volatile int deleteOffsetNum=0;

    /**
     * 处理组退出标志
     */
    private volatile boolean processExit=false;

    /**
     * 输出退出标志
     */
    private volatile boolean outputExit=false;

    /**
     * 运行器线程
     */
    private volatile List<Thread> threadList=new Vector<>();

    /**
     * 输入，处理，输出 3个runner
     */
    private CountDownLatch latch=new CountDownLatch(3);

    /**
     * 新增线程
     * @param thread
     */
    public void addThread(Thread thread) {
        this.threadList.add(thread);
        latch.countDown();
    }

    /**
     * 中断所有线程
     */
    public void interruptRunner(){
        try {
            latch.await();
        } catch (Exception e) {
            log.error("闭锁异常",e);
        }
        for (Thread thread : threadList) {
            thread.interrupt();
        }
    }
}